<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
// use app\admin\Model\Gkdata;
class Yhtype extends Base
{
    public function search()
    {
        $request=new request();
        $sname=$request->post('sname');
        $yid=(int)$sname;
        // dump($ptid);exit;
        if(empty($yid)){
            $data=Db::table('yhtype')->paginate(10);
        }else{
            $data=Db::table('yhtype')->where('yid',$yid)->paginate(10);
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function lookyh(){
        $request=new request();
        $yid=$request->get('id');
        if($yid!=0){
            $data=Db::table('yhtype')->where('yid',$yid)->paginate(10);
        }else{
            $this->error('该商品还没有选择优惠方式','product/index');
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function index()
    {
        $data=Db::table('Yhtype')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }

    // 进入
    public function addpage(){
        return $this->fetch();
    }
    // 添加
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $yname = $request->post("yname");
        $yhman = $request->post("yhman");
        $yhsong = $request->post("yhsong");
        $yhzk = $request->post("yhzk");
        $yhnum = $request->post("yhnum");
        // $yhcun = $request->post("yhcun");
        $startime = $request->post("startime");
        $startime =(int)strtotime(date($startime));
        $endtime = $request->post("endtime");
        $endtime =(int)strtotime(date($endtime));
        
        $data1=[
            "yname"=>$yname,
            "yhman"=>$yhman,
            "yhsong"=>$yhsong,
            "yhzk"=>$yhzk,
            "yhnum"=>$yhnum,
            "startime"=>$startime,
            "endtime"=>$endtime,
            "time"=>time()
        ];
        $re=Db::name('yhtype')->insert($data1);
        if($re){
            $this->success('新增优惠方式成功', 'yhtype/index');
        }else{
            return $this->error('新增优惠方式失败');
        }
    }
    // 进入更改优惠
    public function editpage(){
        $request=new request();
        $id= intval($request->get("id"));
        $data=Db::name('yhtype')->where('yid',$id)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 审核优惠
    
    // 更改优惠
    public  function editad(){
        $request=new request();
        $data1=[];
        $yid=$request->post('yid');
        $data1['yid']=$yid;
        $data=Db::name('yhtype')->where('yid',$yid)->find();
        $yname = $request->post("yname");
        if($yname!=$data['yname']){
            $data1['yname']=$yname;
        }
        $yhman = $request->post("yhman");
        if($yhman!=$data['yhman']){
            $data1['yhman']=$yhman;
        }
        $yhsong = $request->post("yhsong");
        if($yhsong!=$data['yhsong']){
            $data1['yhsong']=$yhsong;
        } 
        $yhnum = $request->post("yhnum");
        if($yhnum!=$data['yhnum']){
            $data1['yhnum']=$yhnum;
        }
        // $yhcun = $request->post("yhcun");
        // if($yhcun!=$data['yhcun']){
        //     $data1['yhcun']=$yhcun;
        // }
        $yhzk = $request->post("yhzk");
        if($yhzk!=$data['yhzk']){
            $data1['yhzk']=$yhzk;
        }
        $endtime = $request->post("endtime");
        $endtime =(int)strtotime(date($endtime));
        if($endtime!=$data['endtime']){
            $data1['endtime']=$endtime;
        }
        $startime = $request->post("startime");
        $startime =(int)strtotime(date($startime));
        if($startime!=$data['startime']){
            $data1['startime']=$startime;
        }
        $data1['time']=time();
        $da=Db::name('yhtype')->update($data1);
        if($da){
            $this->success('更新优惠成功', 'yhtype/index');
        }else{
            return $this->error('更新优惠失败');
        }

    }
    // 删除用户
    public function delyhtype()
    {

        $request=new request();
        $adid=$request->get("id");
        $da=Db::name('yhtype')->delete($adid);
        if($da){
            $this->success('删除成功', 'yhtype/index');
        }
        else{
            return $this->error('删除失败');
        }
    }




}
